/*
 * Created on 2004-01-06
 */
package org.opencube.oms.meta;

import java.util.Date;

import org.opencube.oms.OMSNode;
import org.opencube.util.Util;

/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO: comment
 */
public class DatatypeEnumeration extends OMSNode
{
	/**
	 * The use flag constant: optional = '0'; 
	 */
	public static final char USE_OPTIONAL = '0';

	/**
	 * The use flag constant: required = '1'; 
	 */
	public static final char USE_REQUIRED = '1';

	/**
	 * The use flag constant: defined in the attribute = '2'; 
	 */
	public static final char USE_DEFINED_IN_ATTRIBUTE = '2';

	/* The datatype */
	private Datatype datatype = null;
	/* The use flag */
	private char use = USE_OPTIONAL;
	
	private static final char[] VALID_USES =
	{
		USE_OPTIONAL,
		USE_REQUIRED,
		USE_DEFINED_IN_ATTRIBUTE
	};

	/**
	 * @param datatype
	 * @param id
	 * @param name
	 * @param creatingDate
	 * @param creater
	 * @param modifyingDate
	 * @param modifier
	 * 
	 * @throws OMSMetaDataException
	 */
	DatatypeEnumeration( Datatype datatype, char use, String id, String name, Date creatingDate, String creater, Date modifyingDate, String modifier )
	throws OMSMetaDataException
	{
		super( id, name, creatingDate, creater, modifyingDate, modifier );
		this.datatype = datatype;
		this.use = use;
		datatype.getNamespace().getMetaData().addDatatypeEnumeration( this );
		
		if( datatype == null )
		{
			throw new OMSMetaDataException( "The datatype for the datatype enumeration cannot be null: name = '" + name + "', id = '" + id + "'" );
		}
		if( !Util.inArray( VALID_USES, use ) )
		{
			throw new OMSMetaDataException( "The invalid use flag '" + use + "' for the datatype enumeration: name = '" + name + "', id = '" + id + "'" );
		}
	}

	/**
	 * Retruns the datatype
	 * 
	 * @return Datatype
	 */
	public Datatype getDatatype()
	{
		return datatype;
	}
	
	/**
	 * Returns the use flag
	 * 
	 * @return The use flag value
	 */
	public char getUse()
	{
		return use;
	}
	
	/**
	 * Checks, if the value for this datatype enumeration is optional
	 * 
	 * @return <code>true</code>, if the value for this datatype enumeration is optional
	 */
	public boolean isOptional()
	{
		return use == USE_OPTIONAL;
	}

	/**
	 * Checks, if the value for this datatype enumeration is required
	 * 
	 * @return <code>true</code>, if the value for this datatype enumeration is required
	 */
	public boolean isRequired()
	{
		return use == USE_REQUIRED;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "<datatype_enumeration name=\"" + name + "\" use=\"" + use + "\" id=\"" + id + "\"/>";
	}
}
